//
//  ViewController.swift
//  SwiftDemo
//
//  Created by huan liu on 2018/12/11.
//  Copyright © 2018年 xingweixin. All rights reserved.
//

import UIKit



class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    

    var tableView:UITableView?
    var dataArray:NSArray?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        title = "首页"
        view.backgroundColor = UIColor.white
        
        //初始化数组
        self._initData()
        
        //初始化视图
        self._initTableview()
        
        
    }
    
    func _initData() -> Void {
        
        self.dataArray = [["UIView","UILable"]];
        
        
    }
    
    
    //初始化项目
    func _initTableview() -> Void {
        
        self.tableView = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height), style: UITableView.Style.grouped)
        self.tableView?.delegate = self as UITableViewDelegate
        self.tableView?.dataSource = self as UITableViewDataSource
        self.tableView?.tableFooterView = UIView()
        
        view.addSubview(tableView!)
        
    }
    
    
    //MARK: 代理协议
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return (self.dataArray?.count)!
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        
        return (self.dataArray![section] as! NSArray).count 
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let iden = "cell_iden";
        
        var cell = tableView.dequeueReusableCell(withIdentifier: iden)
        
        if (cell == nil) {
            cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: iden)
        }
        
        cell?.textLabel?.text = (self.dataArray?[indexPath.section] as! NSArray)[indexPath.row] as? String
        cell?.accessoryType = .disclosureIndicator
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        let stringx = (self.dataArray?[indexPath.section] as! NSArray) [indexPath.row] as! String
        
        if stringx == "UILable" {
            
            let sLabelVC = SLabelViewController.init()
            self.navigationController?.pushViewController(sLabelVC, animated: true)
            
        }
        
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let headView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 40))
        headView.backgroundColor = UIColor.cyan
        let label:UILabel = UILabel(frame: CGRect(x: 15, y: 0, width: UIScreen.main.bounds.width-30, height: 40))
        headView.addSubview(label)
        label.backgroundColor = UIColor.clear
        label.textColor = UIColor.black
        label.font = UIFont.systemFont(ofSize: 15)
        label.textAlignment = .center
        let array:NSArray = ["UI控件"];
        label.text = array[section] as? String
        
        return headView
    
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    
    

}

